package com.starwish.starwishblog.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.mapper.PermissionMapper;
import com.starwish.starwishblog.vo.MenuVo;
import com.starwish.starwishblog.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PermissionService {

    @Autowired
    PermissionMapper permissionMapper;

    public List<MenuVo> queryAllMenus(){
        return permissionMapper.selectAllMenus();
    }

    public PageInfo<PermissionVo> queryPermissionsByCons(Integer now, Integer size, String name){
        PageHelper.startPage(now,size);
        List<PermissionVo> permissionList = permissionMapper.selectPermissionsByCons(name);
        return new PageInfo<>(permissionList);
    }
    @Transactional
    public int removePermissionById(Integer id){
        permissionMapper.deleteRoleAndPermissionByPermissionId(id);
        return permissionMapper.deleteByPrimaryKey(id);
    }

    public List<PermissionVo> queryParentPermissions(){
        return permissionMapper.selectParentPermissions();
    }

    public int addPermission(PermissionVo permissionVo){
        return permissionMapper.insert(permissionVo);
    }

    public int modifyPermission(PermissionVo permissionVo) {

        return permissionMapper.updateByPrimaryKey(permissionVo);
    }

    public List<MenuVo> queryMenusByName(String name){
        return permissionMapper.selectMenuByName(name);
    }
}
